package com.sky.controller.admin;

import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.utils.RedisUtils;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@RestController("adminSetmealController")
@RequestMapping("/admin/setmeal")
@Slf4j
@Api(tags = "C端-套餐浏览接口")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private RedisTemplate<String,List<Setmeal>> redisTemplate;

    /**
     * 条件查询
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @Cacheable(cacheNames = "setMeal",key = "#categoryId")
    @ApiOperation("根据分类id查询套餐")
    public Result<List<Setmeal>> list(Long categoryId) {
        Setmeal setmeal = new Setmeal();
        setmeal.setCategoryId(categoryId);
        setmeal.setStatus(StatusConstant.ENABLE);

        List<Setmeal> list = setmealService.list(setmeal);
        return Result.success(list);
    }

    /**
     * 根据套餐id查询包含的菜品列表
     *
     * @param id
     * @return
     */
    @GetMapping("/dish/{id}")
    @ApiOperation("根据套餐id查询包含的菜品列表")
    public Result<List<DishItemVO>> dishList(@PathVariable("id") Long id) {
        List<DishItemVO> list = setmealService.getDishItemById(id);
        return Result.success(list);
    }

    /**
     * 套餐信息分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("套餐分类查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("套餐信息分页查询{}",setmealPageQueryDTO);
//        PageResult pageResult = new PageResult();
//        List<Setmeal> setmeals = new ArrayList<>();
//        如果查询条件是空值，直接去缓存读取套餐全量数据
//        if (setmealPageQueryDTO == null || setmealPageQueryDTO.getCategoryId() == null){
//            Set<String> keys = redisTemplate.keys("setMeal*");
//            setmeals = redisTemplate.opsForValue().get(keys);
//        }
//        查询对应分类的套餐信息
//        if (setmealPageQueryDTO != null && setmealPageQueryDTO.getCategoryId() != null){
//            setmeals = redisTemplate.opsForValue().get("setMeal::" + setmealPageQueryDTO.getCategoryId());
//        }
//        缓存未命中，读取数据库信息
//        if (setmeals == null && setmeals.size() == 0){
//            log.info("缓存未命中，读取数据库信息");
//            pageResult = setmealService.pageInfo(setmealPageQueryDTO);
//            log.info("写入缓存数据");
//            List records = pageResult.getRecords();
//            redisTemplate.opsForValue().set("setMeal",pageResult.getRecords());
//        }
//            pageResult.setTotal(setmeals.size());
//            pageResult.setRecords(setmeals);
        return Result.success(setmealService.pageInfo(setmealPageQueryDTO));
    }

    /**
     * 新增套餐信息
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐信息")
    @CacheEvict(cacheNames = "setMeal",key = "#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐信息{}",setmealDTO);
        setmealService.save(setmealDTO);
        return Result.success();
    }

    /**
     * 套餐起售与停售
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("套餐起售/停售")
    @CacheEvict(cacheNames = "setMeal",allEntries = true)
    public Result startOrStop(@PathVariable Integer status){
        log.info("套餐起售与停售，修改状态为{}",status == 1 ? "起售" : "停售");
        setmealService.startOrStop(status);
        return Result.success();
    }

    /**
     * 根据id获取套餐信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id获取套餐信息")
    public Result getSetMealById(@PathVariable Long id){
        Setmeal setMeal = setmealService.getSetMealById(id);
        return Result.success(setMeal);
    }

    /**
     * 修改套餐信息
     * @return
     */
    @PutMapping
    @ApiOperation("修改套餐信息")
    @CacheEvict(cacheNames = "setMeal",allEntries = true)
    public Result update(@RequestBody SetmealDTO setmealDTO){
        setmealService.updateSetMeal(setmealDTO);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("删除套餐信息")
    @CacheEvict(cacheNames = "setMeal",allEntries = true)
    public Result delete(String ids){
        String[] idList = ids.split(",");
        setmealService.bachDelete(idList);
        return Result.success();
    }

}
